/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.serializable.test;

import java.io.*;

public class SerializableUtils {
    private final static String fp = "d:/SerializableUtils.txt";

    /**
     * 序列化服务
     *
     * @param o
     */
    public static void serializeObj(Serializable o) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fp));
            oos.writeObject(o);
            oos.close();
        } catch (Exception e) {
            throw new RuntimeException("反序列化失败");
        }
    }

    /**
     * 反序列化服务
     *
     * @return
     */
    public static Object deserializeObj() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(fp));
            return ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("");
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        Object person = new Person();
        SerializableUtils.serializeObj((Serializable) person);
        Object obj = SerializableUtils.deserializeObj();
        System.out.println(obj);
        Object person1 = new Person1();

        Object person2 = new Person2();

    }
}
